
import java.io.Serializable;
import java.security.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JOptionPane;

public class User implements Serializable {                                 

    private String name;
    private int iD;
    private String passwordHash;
    private ArrayList<Friend> friendSet;                                      
    private Set<Message> cipherList;
    private int friendCount;
    private static final String SALT = "my-salt-text";                      

    public User(int userCount) {
        name = "";
        passwordHash = "";
        iD = userCount;
        friendSet = new ArrayList<Friend>();                                     
        cipherList = new HashSet<Message>();
        friendCount = 1;
    }

    public User(int userCount, String uName, String uPasswordHash) {
        name = uName;
        passwordHash = uPasswordHash;
        iD = userCount;
        friendSet = new ArrayList<Friend>();
        cipherList = new HashSet<Message>();
        friendCount = 1;
    }

    // displays encrypted right now. update
    public void displayMessages() {
        Iterator iter = cipherList.iterator();
        int lines = 1;
        String output = "Here are your messages:\n";
        while (iter.hasNext()) {
            output += lines + ") " + iter.next().toString() + "\n\n";
            lines++;
        }
        JOptionPane.showMessageDialog(null, output);
    }

    public void addMessage(Message c) {
        cipherList.add(c);
    }

    public void deleteMessage(Message c) {
        cipherList.remove(c);
    }

    public void addFriend(Friend friend) {
        friendSet.add(friend);
        friendCount++;
    }

    public void deleteFriend(Friend removeFriend) {
        if (friendCount > 1) {
            friendSet.remove(removeFriend);
            friendCount--;
        }
    }

    public boolean hasFriend(int fID) {
        Iterator iter = friendSet.iterator();
        while (iter.hasNext()) {
            if (((Friend) iter.next()).getID() == fID) {
                return true;
            }
        }
        return false;
    }

    public String displayFriend() {
        Iterator iter = friendSet.iterator();
        String output = "Here are your Friends:\n";
        while (iter.hasNext()) {
            output += iter.next().toString() + "\n\n";
        }
        return output;
    }

    public Friend findFriend(int id) {
        Iterator iter = friendSet.iterator();
        while (iter.hasNext()) {
            Friend temp = ((Friend) iter.next());
            if (temp.getID() == id) {
                return temp;
            }
        }
        return null;
    }

    public boolean updateFriend(Friend friend) {                                            
        if (hasFriend(friend.getID())) {
            friendSet.set(friend.getID()-1, friend);
            return true;
        }
        return false;
    }

    public boolean setName(String uName) {
        if (validateName(uName)) {
            name = uName;
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public boolean validateName(String name) {
        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i) != ' ' && !(name.charAt(i) >= 65
                    && name.charAt(i) <= 90) && !(name.charAt(i) >= 97
                    && name.charAt(i) <= 122)) {
                return false;
            }
        }
        return true;
    }

    public int getID() {
        return iD;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    // do we need to validate password, character length?
    public boolean setPasswordHash(String iPassword) {
        this.passwordHash = hashPassword(iPassword);
        return true;
    }

    public static String hashPassword(String iPassword) {
        StringBuilder hash = new StringBuilder();

        try {
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byte[] hashedBytes = sha.digest(iPassword.getBytes());
            char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
            for (int idx = 0; idx < hashedBytes.length; ++idx) {
                byte b = hashedBytes[idx];
                hash.append(digits[(b & 0xf0) >> 4]);
                hash.append(digits[b & 0x0f]);
            }
        } catch (NoSuchAlgorithmException e) {
            // handle error here.
        }

        return hash.toString();
    }

    public int getFriendCount() {
        return friendCount;
    }

    @Override
    public String toString() {
        return String.format("Name: %s\nUserID: %d", getName(), getID());
    }
}